import os
import pickle as pkl


class pageranker():
    """
    pageranker not done yet,and all below MAY BE WRONG!!!
    """
    def __init__(self, data_file_name, r_file_name, mid_path_name):
        super(pageranker, self).__init__()
        assert os.path.exists(data_file_name), "no data file named " + data_file_name
        self.mid_path_name = mid_path_name
        self.data_file_name = data_file_name
        if not os.path.exists(mid_path_name):
            os.mkdir(mid_path_name)
        # TODO


        if r_file_name is None:
            self.init_r(r_file_name)
            self.init_r(os.path.join(mid_path_name, 'last_r'))
        self.r_file_name = r_file_name
        self.last_r_name = os.path.join(mid_path_name, 'last_r')

        # k is the dim of a packet
        # n is the dim of pages
        self.k = ...
        self.n = ...

        self.read_meta()
        self.init_r()
        # TODO



    def read_meta(self):

        #############################
        # TODO:
        ...

        #############################

    def set_data_file(self, data_file_name):
        self.data_file_name = data_file_name
        self.read_meta()

    def init_r(self, r_file_name):
        #############################
        # TODO:
        ...

        #############################

    def pageranking(self, max_loops=None):
        #############################
        # TODO:
        ...
        if max_loops is None:
            # loop until converge
            ...
            converges = False
            while not converges:
                self.do_one_mul()
            return True
        else:
            for i in range(max_loops):
                converges = self.do_one_mul()
                if converges:
                    break
            return True

        #############################

    def do_one_iter(self):
        converges = False
        #############################
        # TODO


        #############################
        converges = self.compare_r()
        return converges

    def compare_r(self):
        same = False
        # TODO

        return same
